const app = getApp()

Page({

  /**
   * 页面的初始数据
   */
  data: {
    orderList: [],
    currentTab: 1,
    pageReady: false,
    dataLoading:false,
    pullDownLoad:false,
    inPay:false,
    len:30,
    start:0,
    pic_domain:""
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      currentTab: options.tab
    })
  },
  onShow: function(){
    this.getOrderList()
  },
  //tab切换
  navbar(e) {
    this.setData({
      currentTab: e.currentTarget.dataset.type,
      pageReady: false,
      pullDownLoad:false
    })
    this.getOrderList()
  },
  //获取列表
  getOrderList:function(){
    var that = this
    wx.request({
      url: app.urlData.getURL + '/index.php/Weshop/SApi/index/m/getOrderList/key/' + app.urlData.key + '/wid/' + app.urlData.wid +'/sign/'+ wx.getStorageSync('sign'),
      method: 'get',
      header: { "Content-Type": "application/x-www-form-urlencoded" },
      data: {
        len:that.data.len,
        status: that.data.currentTab - 1,
        start:that.data.start,
      },
      success: (res) => {
        that.setData({
          orderList: res.data.datas.orderList,
          pic_domain: res.data.datas.pic_domain
        })
        setTimeout(function () {
          that.setData({
            pageReady: true
          })
        }, 500)
      }
    })
  },
  /* 下拉加载 */
  nextPage:function(){
    var that = this
    if (that.data.dataLoading == false && that.data.pullDownLoad == false){
      that.setData({
        dataLoading:true
      });
      wx.showLoading({
        title: '加载中',
      })
      let len = that.data.len
      let sums = len + len
      wx.request({
        url: app.urlData.getURL + '/index.php/Weshop/SApi/index/m/getOrderList/key/' + app.urlData.key + '/wid/' + app.urlData.wid + '/sign/' + wx.getStorageSync('sign'),
        method: 'get',
        data: {
          start:len,
          len: sums
        },
        success: (res) => {
          if (res.data.ordes){
            if (res.data.ordes.length != 0) {
              that.setData({
                orderList: that.data.orderList.concat(res.data),
                len: sums
              })
            } else {
              wx.showToast({
                title: '到底啦~',
                icon: 'success',
                duration: 2000
              });
              that.setData({
                pullDownLoad: true
              })
            }
          }
          setTimeout(function(){
            that.setData({
              dataLoading: false
            })
          },1800)
        }
      })
      wx.hideLoading();
    }
  },
  /*删除订单接口 */
  deleOrder:function(e){
    var that = this
    let sign = wx.getStorageSync("sign")
    wx.showModal({
      title: "提示",
      content: "是否确定取消订单？",
      success: function (res) {
        if(res.confirm){
          wx.request({
            url: app.urlData.getURL + "/index.php/Weshop/SApi/index/m/cancelOrder/key/" + app.urlData.key + "/wid/" + app.urlData.wid + "/sign/" + sign,
            method: 'get',
            data: {
              order_id: e.currentTarget.dataset.index
            },
            success: (res) => {
              var list = that.data.orderList
              list.splice(e.currentTarget.dataset.index, 1)
              if (res.data.status == 1){
                that.setData({
                  orderList: list,
                })
                wx.showToast({
                  title: res.data.msg,
                  icon: 'success',
                  duration: 800
                });
                that.getOrderList();
              }
            }
          })
        }
      }
    });
  },
  /*支付接口 */
  wxplay(e) {
    var that = this
    let order_id = e.currentTarget.dataset.id
    let sign = wx.getStorageSync("sign")
    if (that.data.inPay == false) {
      that.setData({
        inPay: true
      })
      wx.request({
        url: app.urlData.getURL + "/index.php/Weshop/SApi/index/m/saveOrderByOrderId/key/" + app.urlData.key + "/wid/" + app.urlData.wid + "/sign/" + sign,
        method: 'get',
        data: {
          order_id: order_id,
          vip_id: wx.getStorageSync('vip_id')
        },
        success: (res) => {
          if (res.data.status == -1) {
            wx.showModal({
              title: '提示',
              showCancel: false,
              content: res.data.msg
            })
          } else {
            wx.requestPayment({
              'appId': res.data.datas.appId,
              'nonceStr': res.data.datas.nonceStr,
              'package': res.data.datas.package,
              'signType': res.data.datas.signType,
              'paySign': res.data.datas.paySign,
              'timeStamp': res.data.datas.timeStamp,
              'success': function (res) {
                wx.showModal({
                  title: '恭喜您',
                  showCancel: false,
                  content: '已成功购买，请在系统消息中查看！',
                  success: function (res) {
                    //这里写支付成功后操作
                    //wx.navigateBack();
                  }
                })
              }
            })
          }
          that.setData({
            inPay: false
          })
        }
      })
    }
  },
  //确认订单
  confOrder: function (e) {
    var that = this
    wx.showModal({
      title: "提示",
      content: "是否确定收取订单？",
      success: function (res) {
        wx.request({
          url: app.urlData.getURL + "/index.php/Weshop/SApi/index/m/confirmOrder/key/" + app.urlData.key + "/wid/" + app.urlData.wid + "/sign/" + wx.getStorageSync("sign"),
          method: 'POST',
          header: { "Content-Type": "application/x-www-form-urlencoded" },
          data: {
            order_id: e.currentTarget.dataset.id
          },
          success: (res) => {
            var list = that.data.orderList;
            list.splice(e.currentTarget.dataset.index, 1)
            that.setData({
              orderList: list
            })
            wx.showToast({
              title: res.data.msg,
              icon: 'success',
              duration: 800
            });
          }
        })
      }
    });
  }
})
